package com.bonc.rms.service.impl.tms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bonc.rms.dao.tms.IAreaDistributionDao;
import com.bonc.rms.service.tms.IAreaDistributionService;


@Service
public class AreaDistributionServiceImpl implements IAreaDistributionService {

	@Autowired
	IAreaDistributionDao areaDistributionDao;

	private List<String> provs = null;
	private Map<String, List<String>> provCity = null;
	
	@Override
	public Map<String, Integer> getAreaDistribution(Integer depId) {
		
		initRegionData();
		
		Map<String, Integer> areaDistribution = new HashMap<String, Integer>();
		List<String> talentAreaList = areaDistributionDao.getTalentArea(depId);
		
		for(String pro : provs) {
			areaDistribution.put(locationAnalyze(pro), 0);
		}
		
		outerLoop:
		for(String ta : talentAreaList) {
			if(ta == null || "".equals(ta)) {
				continue;
			}
			
			for(String pro : provs) {
				if(ta.contains(pro)) {
					pro = locationAnalyze(pro);
					Integer num = areaDistribution.get(pro);
					areaDistribution.put(pro, num == null ? 1 : num + 1);
					continue outerLoop;
				}
			}
			
			Set<String> provList = provCity.keySet();
			List<String> cityList = null;
			for(String prov : provList) {
				cityList = provCity.get(prov);
				prov = locationAnalyze(prov);
				for(String city : cityList) {
					if(ta.contains(city)) {
						Integer num = areaDistribution.get(prov);
						areaDistribution.put(prov, num == null ? 1 : num + 1);
						continue outerLoop;
					}
				}
			}
		}
		
		return areaDistribution;
	}
	
	private void initRegionData() {
		List<Map<String, Object>> provMapList = areaDistributionDao.getProv();
		List<Map<String, Object>> cityMapList = areaDistributionDao.getCity(null);
		provs = new ArrayList<String>();
		provCity = new HashMap<String, List<String>>();
		List<String> cities = null;
		for(Map<String, Object> prov : provMapList) {
			Integer provId = ((Long) prov.get("id")).intValue();
			String provName = (String) prov.get("name");
			provs.add(provName);
			cities = new ArrayList<String>();
			for(Map<String, Object> city : cityMapList) {
				if(((Integer) city.get("parent_id")) == provId) {
					cities.add((String) city.get("name"));
				}
			}
			provCity.put(provName, cities);
		}
	}
	
	private String locationAnalyze(String location){
		if(location.contains("黑龙江")){
			return "heilongjiang";
		} else if(location.contains("吉林")){
			return "jilin";
		} else if(location.contains("辽宁")){
			return "liaoning";
		} else if(location.contains("河北")){
			return "hebei";
		} else if(location.contains("河南")){
			return "henan";
		} else if(location.contains("山东")){
			return "shandong";
		} else if(location.contains("江苏")){
			return "jiangsu";
		} else if(location.contains("山西")){
			return "shanxi1";
		} else if(location.contains("陕西")){
			return "shanxi2";
		} else if(location.contains("甘肃")){
			return "gansu";
		} else if(location.contains("四川")){
			return "sichuan";
		} else if(location.contains("青海")){
			return "qinghai";
		} else if(location.contains("湖南")){
			return "hunan";
		} else if(location.contains("湖北")){
			return "hubei";
		} else if(location.contains("江西")){
			return "jiangxi";
		} else if(location.contains("安徽")){
			return "anhui";
		} else if(location.contains("浙江")){
			return "zhejiang";
		} else if(location.contains("福建")){
			return "fujian";
		} else if(location.contains("广东")){
			return "guangdong";
		} else if(location.contains("广西")){
			return "guangxi";
		} else if(location.contains("贵州")){
			return "guizhou";
		} else if(location.contains("云南")){
			return "yunnan";
		} else if(location.contains("海南")){
			return "hainan";
		} else if(location.contains("内蒙古")){
			return "neimenggu";
		} else if(location.contains("新疆")){
			return "xinjiang";
		} else if(location.contains("台湾")){
			return "taiwan";
		} else if(location.contains("宁夏")){
			return "ningxia";
		} else if(location.contains("西藏")){
			return "xizang";
		} else if(location.contains("北京")){
			return "beijing";
		} else if(location.contains("上海")){
			return "shanghai";
		} else if(location.contains("天津")){
			return "tianjin";
		} else if(location.contains("重庆")){
			return "chongqing";
		} else if(location.contains("香港")){
			return "xianggang";
		} else if(location.contains("澳门")){
			return "aomen";
		}
		
		return location;
	}

}
